package com.web.base.wechat.provider;

import java.util.function.Function;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.cache.WechatTemplateCache;
import com.web.base.wechat.exception.WechatTemplateException;
import com.web.base.wechat.facade.WechatTemplateFacade;
import com.web.base.wechat.message.WechatTemplateRequest;
import com.web.base.wechat.message.WechatTemplateResponse;
import com.web.base.wechat.model.WechatTemplate;
import com.web.base.wechat.service.WechatTemplateService;
import com.web.base.wechat.util.WechatTemplateConvertor;
import com.web.core.async.redis.CacheRefreshSender;

@Component
public class WechatTemplateProvider implements WechatTemplateFacade
{
	@Autowired
	private WechatTemplateService wechatTemplateService;
	@Resource
	private WechatTemplateCache wechatTemplateCache;
	@Resource
	private CacheRefreshSender cacheRefreshSender;
	
	public WechatTemplateProvider()
	{
	}

	@Override
	public WechatTemplateResponse add(WechatTemplateRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		return this.process(request, wechatTemplateService::save);
	}

	@Override
	public WechatTemplateResponse update(WechatTemplateRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		return this.process(request, wechatTemplateService::save);
	}
	
	private WechatTemplateResponse process(WechatTemplateRequest request, Function<WechatTemplate, WechatTemplate> f)
	{
		WechatTemplate template = WechatTemplateConvertor.toWechatTemplate(request);
		template = f.apply(template);
		cacheRefreshSender.send(CacheRefreshSender.WECHAT_TEMPLATE_CACHE, template.getId());
		WechatTemplateResponse response = WechatTemplateConvertor.toResponse(template);
		return response;
	}

	@Override
	public WechatTemplateResponse get(Integer id)
	{
		if (id == null)
			throw new WechatTemplateException("ID不能为空");
		
		WechatTemplate template = wechatTemplateCache.get(id);
		WechatTemplateResponse response = WechatTemplateConvertor.toResponse(template);
		return response;
	}

	@Override
	public Page<WechatTemplateResponse> listPage(WechatTemplateRequest request, int start, int size)
	{
		WechatTemplate template = WechatTemplateConvertor.toWechatTemplate(request);
		Page<WechatTemplate> page = new Page<>(size);
		page.setCurPage(start);
		page = wechatTemplateService.list(template, page);
		Page<WechatTemplateResponse> responsePage = WechatTemplateConvertor.toResponse(page);
		return responsePage;
	}

	@Override
	public WechatTemplateResponse getByCode(String wechatCode, String code)
	{
		if (wechatCode == null || wechatCode.isEmpty())
			throw new WechatTemplateException("微信编号不能为空");
		
		if (code == null || code.isEmpty())
			throw new WechatTemplateException("编号不能为空");
		
		WechatTemplate template = wechatTemplateCache.getByCode(wechatCode, code);
		WechatTemplateResponse response = WechatTemplateConvertor.toResponse(template); 
		return response;
	}
}
